
allPowers = function(parameterName,  parSeq,allcoefs, CommonCancer,
  SimulationTime, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate) {        

 coefAllresult = list()
 
 coefAllresult$GenoMiss = seqPower("GenoMiss", c(0.01, 0.1, 0.25),   
  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson = sqrt(0.4875), 
sdGroup =sqrt(0.0975),size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  100, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)
 
coefAllresult$EnvMiss = seqPower("EnvMiss", c(0.01, 0.1, 0.25),   
  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson = sqrt(0.4875), 
sdGroup =sqrt(0.0975),size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  100, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)

 coefAllresult$probGeno = seqPower("probGeno", c(0.05, 0.2, 0.33, 0.5),   
  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson = sqrt(0.4875), 
sdGroup =sqrt(0.0975),size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  100, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)
 

 coefAllresult$probEnv= seqPower("probEnv", c(0.05, 0.1, 0.2, 0.33, 0.5),   
  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson = sqrt(0.4875), 
sdGroup =sqrt(0.0975), size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  SimulationTime, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)

 coefAllresult$size= seqPower("size", c(75000,100000,150000,300000),   
  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson = sqrt(0.4875), 
sdGroup =sqrt(0.0975), size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  SimulationTime, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)

 coefAllresult$geno= seqPower("geno", log(c(1.05,1.1,1.2,1.5)),   
 c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson =sqrt(0.4875), 
sdGroup =sqrt(0.0975), size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  SimulationTime, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)
  
 coefAllresult$env= seqPower("env", log(c(1.1, 1.2, 1.5 ,2)),   
  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson = sqrt(0.4875), 
sdGroup =sqrt(0.0975), size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  SimulationTime, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)

 coefAllresult$geno_env= seqPower("geno:env", log(c(1.1, 1.2, 1.5 ,2)),   
  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson =sqrt(0.4875), 
sdGroup =sqrt(0.0975), size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  SimulationTime, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)

 coefAllresult$sdPerson= seqPower("sdperson", c(sqrt(0.585),sqrt(0.4875),sqrt(0.43875)),   
  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson =sqrt(0.4875), 
sdGroup =sqrt(0.0975), size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  SimulationTime, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)

  coefAllresult$sdGroup= seqPower("sdGroup", c(0,sqrt(0.0975),sqrt(0.14625)),   
  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), sdPerson = sqrt(0.4875), 
sdGroup = sqrt(0.0975), size=150000, GenoMiss=0.1, EnvMiss=0.1), CommonCancer,
  SimulationTime, agegroup, lambdaF, 
  lambdaM, ageCancer, Enrollmentprobs,Followup, YesNoRate, NoYesRate)


}

allPowers( parameterName, parSeq,
allcoefs =  c(probGeno = 0.1, probEnv = 0.33, geno=log(1.5), env=log(1.2), "geno:env"=log(2), 
sdPerson = sqrt(0.4875), sdGroup =sqrt(0.0975), size=150000, GenoMiss=0.1, EnvMiss=0.1)     ,
CommonCancer=c("Breast", "Prostate","Colon","Lung", "Stomach"), 
SimulationTime=100,
agegroup=c(35,40,45,50,55,60,65, 70),  
lambdaF =list(x=seq(30, 90, by=5), y= exp((-0.5)*0.585)*c(116.4,169.7,272.6,401.8,555.4,746.8,1003.2,1254.8,1528.7,1741.4,
1903.1,1950.2,1922.5) / 100000)  ,
lambdaM=list(x=seq(30,90, by=5), y=exp((-0.5)*0.585)*c(61.8,81.2,123.6,247.8,466.8,877.5,1403.7,2096.6,2598.4,2887.2,
3086.4, 3156.3, 2927.5)/ 100000)  ,
ageCancer=seq(30, 90, by=5)  ,
Enrollmentprobs=c(20000, 40000, 50000, 40000)  ,
Followup=c(5,10,20,30) ,
YesNoRate=0.2, NoYesRate=0.00045 )


newfunction = function() {

nnrow=length(p)*length(covariate)*(length(CommonCancer)-1)
par(mfrow=c(nnrow/4,4))
for (Signif in 1:length(p)) {
   for (Cov in 1: length(coefs)){
      for (CancerType in 1:(length(CommonCancer)-1)){
temp=AllResult[,, covariate[Cov], CommonCancer[CancerType], as.character(p[Signif])]

matplot(probGeno, temp, type="o", lty=1, pch=16, col=thecolours)
legend(120000, 0.6, lty=1, col=thecolours, legend=c("5 years","10 years","20 years","30 years"))
title (main=paste(CommonCancer[CancerType],coefs[Cov],p[Signif]))
      }
   }
}

}


thecolors=1:3
ThePower=t(coefAllresult$size[,,"geno","Breast","0.05"] )
matplot(Followup, ThePower, type="o", lty=1:3, pch=16, col=thecolors, xlab="Follow-up Years"
,ylab="The Power", main="The Power to Detect Genetic Effect for the Cases of Breast Cancer")
legend(22, 0.7, lty=1:4,col=thecolors,cex=1.1,legend=c("size=75000","size=100000","size=150000"))


thecolors=1:4
ThePower=coefAllresult$size[,,"geno","Breast","0.05"] 
matplot(size, ThePower, type="o", lty=1:4, pch=16, col=thecolors, xlab="Sample Size"
,ylab="The Power", main="The Power to Detect Genetic Effect for the Cases of Breast Cancer")
legend(115000, 0.60, lty=1:5,ncol=2,col=thecolors,cex=1.0,legend=c("5 years","10 years","20 years","30 years"))


save(coefAllresult, file="Allresult.RData")
parameters = list(
	   geno=log(1.5), env=log(1.25), "geno:env"=log(2),
		 size=150000, probGeno=0.2, 
		 probEnv=0.3, genoMiss= 0.1,
		 envMiss = 0.1, oneEnvPerCommunity =F,
     cancerMissRate=0.2, falseCancerRate=0.00045, 
		 sdGroup=propGroupSD * totalSD, 
		 sdPerson=sqrt(1-propGroupSD^2)*totalSD, 
		 Enrollmentprobs = c(20000, 40000, 50000, 40000), 
		 agerange = c(35, 69),
		 Followup = c(5, 10, 20, 30),
		 Ncommunity =  50,
		 EqualCommunity=T
)
DiffCommunities_equal = seqPowerList(varying=list("Ncommunity"=c(30,50,75)),
	parameters=parameters, CommonCancer=c("Breast","Colon","Lung","Prostate"), 
	SimulationTime=100, lambda=lambda,
	populationData=populationData,deathData=deathData, DiffCancer) 
save(DiffCommunities_equal, file="DiffCommunities_equal_4cancers")	

parameters = list(
	   geno=log(1.5), env=log(1.25), "geno:env"=log(2),
		 size=150000, probGeno=0.2, 
		 probEnv=0.3, genoMiss= 0.1,
		 envMiss = 0.1, oneEnvPerCommunity =F,
     cancerMissRate=0.2, falseCancerRate=0.00045, 
		 sdGroup=propGroupSD * totalSD, 
		 sdPerson=sqrt(1-propGroupSD^2)*totalSD, 
		 Enrollmentprobs = c(20000, 40000, 50000, 40000), 
		 agerange = c(35, 69),
		 Followup = c(5, 10, 20, 30),
		 Ncommunity =  50,
		 EqualCommunity=F
)
DiffCommunities_noequal = seqPowerList(varying=list("Ncommunity"=c(30,50,75)),
	parameters=parameters, CommonCancer=c("Breast","Colon","Lung","Prostate"), 
	SimulationTime=100, lambda=lambda,
	populationData=populationData,deathData=deathData, DiffCancer) 
	
save(DiffCommunities_noequal, file="DiffCommunities_noequal_4cancers")	